package com.minminaya.mediaservice.Service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.minminaya.mediaservice.Bean.SongBean;
import com.minminaya.mediaservice.utils.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/** 111
 * Created by NIWA on 2017/3/17.
 */

public class MediaService extends Service {
    private MediaPlayer mediaPlayer = new MediaPlayer();
    private SongBean head;
    @Override
    public void onCreate() {
        super.onCreate();
//        File[] files=FileUtils.getDbFileNames();
//        Log.i("hello",files.toString());
        File [] names=FileUtils.getMp3FileNames();
        if(names!=null){
            SongBean pre=null;
            head=pre=new SongBean(names[0].getAbsolutePath(),null,null);
            for (int i=1;i<names.length;i++){
                pre=new SongBean(names[i].getAbsolutePath(),null,pre);
                pre.getPre().setNext(pre);
            }
            head.setPre(pre);
            pre.setNext(head);
            loadSouce(head);
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        return new BinderExd();
    }
    public class BinderExd extends Binder
    {
        public void play(){
            if (!mediaPlayer.isPlaying()) {
                //如果还没开始播放，就开始
                mediaPlayer.start();
            }
        }
        public void pause() {
            if (mediaPlayer.isPlaying()) {
                //如果还没开始播放，就开始
                mediaPlayer.pause();
            }
        }
        /**
         * 下一首
         */
        public void next() {
            if(mediaPlayer != null&&head!=null){
                mediaPlayer.reset();
                head=head.getNext();
                loadSouce(head);
                //这里的if只要是为了不让歌曲的序号越界，因为只有4首歌
                play();
            }
        }
        public void reset() {
            if (!mediaPlayer.isPlaying()) {
                //如果还没开始播放，就开始
                mediaPlayer.reset();
            }
        }
        /**
         * 关闭播放器
         */
        public void close() {
            if (mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.release();
            }
            gcc();
        }
        /**
         * 上一首
         */
        public void precious() {
            if(mediaPlayer != null&&head!=null){
                mediaPlayer.reset();
                head=head.getPre();
                loadSouce(head);
                play();
            }
        }
        /**
         * 回收垃圾测试
         */
        public void gcc() {
            while(head!=null) {
                SongBean tmp=head;
                tmp.setNext(null);
                tmp.setPre(null);
                head=head.getNext();
            }
            System.gc();
        }

        /**
         * 获取歌曲长度
         **/
        public int getProgress() {
            return mediaPlayer.getDuration();
        }
        /**
         * 获取播放位置
         */
        public int getPlayPosition() {
            return mediaPlayer.getCurrentPosition();
        }
        /**
         * 播放指定位置
         */
        public void seekToPositon(int msec) {
            mediaPlayer.seekTo(msec);
        }

    }
    /**
     * 添加file文件到MediaPlayer对象并且准备播放音频
     */
    private void loadSouce(SongBean song) {
        //获取文件路径
        try {
            //此处的两个方法需要捕获IO异常
            //设置音频文件到MediaPlayer对象中
            mediaPlayer.setDataSource(song.getName());
            //让MediaPlayer对象准备
            mediaPlayer.prepare();
        } catch (IOException e) {
        }
    }
}
